package com.yydm.servicesms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yydm.servicesms.service.SmsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * @Classname SmsServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/7/8 10:51
 * @Created by liu25
 */
@Service
public class SmsServiceImpl implements SmsService {
    //    发送短信的方法
    @Override
    public Boolean send(HashMap<String, Object> map, String phone) {

        if (StringUtils.isEmpty(phone)) return false;
        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI5tMYnwt1GGNoDMdJbCTA",
                        "I1cbg8ahhGfe7spQ292p1YepoGFP5X");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

//        设置发送的相关参数
//        手机号
        request.putQueryParameter("PhoneNumbers", phone);
//        设置阿里云 签名名称
        request.putQueryParameter("SignName", "阿里云短信测试");
//        设置阿里云 模板code
        request.putQueryParameter("TemplateCode", "SMS_154950909");
//        验证码数据,转换json数据传递
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

//        最终发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
